import 'package:backend.core/core/domain/entities/google_play/google_play_purchased_item_acknowledgement_status.dart';
import 'package:backend.core/core/domain/entities/google_play/google_play_purchased_subscription_info_cancellation_info.dart';
import 'package:backend.core/core/domain/entities/google_play/google_play_purchased_subscription_info.dart';
import 'package:backend.core/core/domain/entities/google_play/google_play_purchased_subscription_info_pause_info.dart';
import 'package:backend.purchases/domain/entities/google_play_api/google_play_api_purchased_item.dart';
import 'package:backend.purchases/domain/helpers/google_play/create_google_play_purchased_subscription_info_cancellation_info_from_api_one.dart';
import 'package:backend.purchases/domain/helpers/google_play/create_google_play_purchased_subscription_info_pause_info_from_api_one.dart';
import 'package:backend.purchases/domain/helpers/google_play/create_google_play_purchased_subscription_info_price_change_status_from_api_one.dart';

abstract class CreateGooglePlayPurchasedSubscriptionInfoFromApiOne {
  GooglePlayPurchasedSubscriptionInfo call({
    required GooglePlayApiPurchasedSubscription apiPurchasedSubscription,
    required String purchaseToken,
    required String purchaseItemSku,
    required String googlePlayUserId,
  });
}

class CreateGooglePlayPurchasedSubscriptionInfoFromApiOneImpl
    implements CreateGooglePlayPurchasedSubscriptionInfoFromApiOne {
  const CreateGooglePlayPurchasedSubscriptionInfoFromApiOneImpl({
    required CreateGooglePlayPurchasedSubscriptionInfoPriceChangeStatusFromApiOne
        createGooglePlayPurchasedSubscriptionInfoPriceChangeStatusFromApiOne,
    required CreateGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne
        createGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne,
    required CreateGooglePlayPurchasedSubscriptionInfoPauseInfoFromApiOne
        createGooglePlayPurchasedSubscriptionInfoPauseInfoFromApiOne,
  })  : _createGooglePlayPurchasedSubscriptionInfoPriceChangeStatusFromApiOne =
            createGooglePlayPurchasedSubscriptionInfoPriceChangeStatusFromApiOne,
        _createGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne =
            createGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne,
        _createGooglePlayPurchasedSubscriptionInfoPauseInfoFromApiOne =
            createGooglePlayPurchasedSubscriptionInfoPauseInfoFromApiOne;

  final CreateGooglePlayPurchasedSubscriptionInfoPriceChangeStatusFromApiOne
      _createGooglePlayPurchasedSubscriptionInfoPriceChangeStatusFromApiOne;
  final CreateGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne
      _createGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne;
  final CreateGooglePlayPurchasedSubscriptionInfoPauseInfoFromApiOne
      _createGooglePlayPurchasedSubscriptionInfoPauseInfoFromApiOne;

  @override
  GooglePlayPurchasedSubscriptionInfo call({
    required GooglePlayApiPurchasedSubscription apiPurchasedSubscription,
    required String purchaseToken,
    required String purchaseItemSku,
    required String googlePlayUserId,
  }) {
    final priceChangeStatus = _createGooglePlayPurchasedSubscriptionInfoPriceChangeStatusFromApiOne(
      apiPriceChangeStatus: apiPurchasedSubscription.priceChangeStatus,
    );

    GooglePlayPurchasedSubscriptionInfoCancellationInfo? cancellationInfo;

    if (apiPurchasedSubscription.cancellationInfo != null) {
      cancellationInfo = _createGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne(
        apiCancellationInfo: apiPurchasedSubscription.cancellationInfo!,
      );
    }

    GooglePlayPurchasedSubscriptionInfoPauseInfo? pauseInfo;

    if (apiPurchasedSubscription.pauseInfo != null) {
      pauseInfo = _createGooglePlayPurchasedSubscriptionInfoPauseInfoFromApiOne(
        apiPauseInfo: apiPurchasedSubscription.pauseInfo!,
      );
    }

    final GooglePlayPurchasedItemAcknowledgementStatus acknowledgementStatus;

    if (apiPurchasedSubscription.acknowledged) {
      acknowledgementStatus = GooglePlayPurchasedItemAcknowledgementStatus.acknowledged;
    } else {
      acknowledgementStatus = GooglePlayPurchasedItemAcknowledgementStatus.yetToBeAcknowledged;
    }


    return GooglePlayPurchasedSubscriptionInfo(
      purchaseItemSku: purchaseItemSku,
      purchaseToken: purchaseToken,
      googlePlayUserId: googlePlayUserId,
      test: apiPurchasedSubscription.test,
      startTime: apiPurchasedSubscription.startTime,
      autoRenewalEnabled: apiPurchasedSubscription.autoRenewalEnabled,
      acknowledgementStatus: acknowledgementStatus,
      priceChangeStatus: priceChangeStatus,
      cancellationInfo: cancellationInfo,
      pauseInfo: pauseInfo,
    );
  }
}
